<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>weibo 爬虫</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        /* 关闭chrome number input 的上下键选择 */
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }

        /* 关闭firefox number input 的上下键选择 */
        input[type="number"] {
            -moz-appearance: textfield;
        }

        a {
            color: #fff;
            text-decoration: none;
        }

        #app {
            width: 100%;
            height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            text-align: center;
            padding-top: 100px;
            background: linear-gradient(190deg, #7f7fd5, #86a8e7, #91eae4);
        }

        .input_box {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 30px 0 15px;
            width: 600px;
            border-radius: 10px;
            background-color: rgba(255, 255, 255, .5);

        }

        .input_box input {
            height: 50px;
            width: 80%;
            border-radius: 10px;
            margin: 5px;
            padding-left: 10px;
            border: none;
            outline: none;
            font-size: 18px;
        }

        .input_box h2 {
            margin-bottom: 10px;
        }

        .input_box button {
            margin-top: 10px;
            padding: 10px;
            border: none;
            outline: none;
            border-radius: 5px;
            background-color: #4286f4;
            box-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
            color: #fff;
        }

        .input_box button:hover {
            background-color: #fdbb2d;
        }

        .message_box {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
            padding: 20px;
            border-radius: 10px;
            background-color: rgba(255, 255, 255, .7);
            letter-spacing: 1px;
            font-weight: 800;
            color: green;
        }

        .title {
            margin-bottom: 5px;
            color: red;
        }

        .button_box {
            display: flex;
            margin-top: 30px;
            color: #fff;
        }

        .button_box a {
            margin: 0 10px;
            padding: 10px;
            display: block;
            background-color: #4286f4;
            box-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
            color: #fff;
            border-radius: 5px;
        }

        .button_box a:nth-child(1):hover {
            background-color: #96c93d;
        }

        .button_box a:nth-child(2):hover {
            background-color: #ef3b36;
        }
    </style>
</head>

<body>
    <div id="app">

        <div class="input_box">
            <h2>Weibo 爬虫</h2>
            <input type="text" placeholder="输入您需要查找的关键字" v-model="keyword">
            <input type="number" placeholder="非必选,爬取页面的页面数,默认是8,最长16" v-model="pageNum">
            <button @click="crawlerData">点击开始爬取数据</button>
        </div>

        <div class="message_box">
            <span class="title" v-show="title">{{title}}</span>
            <span>{{message}}</span>
        </div>

        <div class="button_box">
            <a href="http://localhost:8080/getExcel">点击导出Excel</a>
            <a href="http://localhost:8080/getJson" target="_blank">点击查看JSON</a>
        </div>

    </div>
</body>

<script>
    var app = new Vue({
        el: '#app',
        data: {
            keyword: "",
            pageNum: null,
            message: "欢迎使用, 请先爬取数据,再导出Excel / 查看JSON",
            title: null
        },
        methods: {
            crawlerData() {

                if (this.keyword.length == 0 || this.keyword.match(/^\s+$/)) {
                    alert("请输入合法的关键字")
                    return;
                }
                if (this.pageNum == null) {
                    this.pageNum = 8
                }
                if (this.pageNum < 1 || this.pageNum > 16) {
                    this.title = "您输入的pageNum长度过长,已重置为8"
                    this.pageNum = 8
                }



                this.message = "正在爬取中..... 请等待"
                axios.get(`http://localhost:8080/crawler/${this.keyword}/${this.pageNum}`)
                    .then((res) => {
                        if (res.data.code != 200) {
                            this.title = res.data.message;
                            this.message = "欢迎使用, 请先爬取数据,再导出Excel / 查看JSON";
                        } else {
                            this.message = res.data.message;
                        }

                    })
            },
            exportExcel() {
                axios.get(`http://localhost:8080/getExcel`)
                    .then((res) => {
                        if (res.data.code != 200) {
                            this.title = res.data.message;
                            this.message = "欢迎使用, 请先爬取数据,再导出Excel / 查看JSON";
                        } else {
                            this.message = res.data.message;
                        }
                    })
            }

        }
    })
</script>

</html>